#include "Game_Init.h"

#include "../Game.h"
#include "../GameControl.h"
#include "../../tools/Log.h"

#include <cstring>

string Game_Init::command = "init game";

string Game_Init::toString() { return string(serialize().data); }

Game_Init::Game_Init(unsigned long Seed)
{
	seed = Seed;
}

SerializedGameCommand Game_Init::serialize() const
{
	SerializedGameCommand cmd;
	sprintf(cmd.data, "%s %lu %lu", command.c_str(), seed, timestamp);
	return cmd;
}

Game_Init* Game_Init::interpret(const SerializedGameCommand& sgc)
{
	if (strncmp(sgc.data, command.c_str(), command.size()) == 0)
	{
		Game_Init* cmd = new Game_Init();
		if(sscanf(sgc.data, "init game %lu %lu", &(cmd->seed), &(cmd->timestamp)) == 2)
			return cmd;
	}
	
	return NULL;
}

Game_Init* Game_Init::interpret(const char* line)
{
	if (strncmp(line, command.c_str(), command.size()) == 0)
	{
		Game_Init* cmd = new Game_Init();
		if(sscanf(line, "init game %lu %lu", &(cmd->seed), &(cmd->timestamp)) == 2)
			return cmd;
		else if(sscanf(line, "init game %lu", &(cmd->seed)) == 1)
			return cmd;
		
		lout << Game_Init::command << " error: Invalid arguments" << endl;
		delete cmd;
	}
	
	return NULL;
}

void Game_Init::onExecute()
{
	// -- BEGIN CUSTOM CODE REGION -- //
	Game::Instance()->init_game(seed);
	// -- END CUSTOM CODE REGION -- //
}
